{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-06-29T07:22:57.680038Z",
     "start_time": "2025-06-29T07:22:57.661347Z"
    }
   },
   "source": [
    "# 批量梯度下降法\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 29
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "$$\n",
    "y = 4 + 3x + noise\n",
    "$$"
   ],
   "id": "8a6ff5c8837b2c97"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-29T07:22:57.774010Z",
     "start_time": "2025-06-29T07:22:57.757056Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建数据集X,y\n",
    "np.random.seed(1)\n",
    "X = 2 * np.random.rand(100, 1)\n",
    "\n",
    "# 线性模型 y = 4 + 3 * X + noise\n",
    "y = 4 + 3 * X + np.random.randn(100, 1)\n",
    "\n",
    "X_b = np.c_[np.ones((100, 1)), X]"
   ],
   "id": "73701f57bcbcc827",
   "outputs": [],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-29T07:22:57.820885Z",
     "start_time": "2025-06-29T07:22:57.805925Z"
    }
   },
   "cell_type": "code",
   "source": "X",
   "id": "536ef669a8467a39",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8.34044009e-01],\n",
       "       [1.44064899e+00],\n",
       "       [2.28749635e-04],\n",
       "       [6.04665145e-01],\n",
       "       [2.93511782e-01],\n",
       "       [1.84677190e-01],\n",
       "       [3.72520423e-01],\n",
       "       [6.91121454e-01],\n",
       "       [7.93534948e-01],\n",
       "       [1.07763347e+00],\n",
       "       [8.38389029e-01],\n",
       "       [1.37043900e+00],\n",
       "       [4.08904499e-01],\n",
       "       [1.75623487e+00],\n",
       "       [5.47751864e-02],\n",
       "       [1.34093502e+00],\n",
       "       [8.34609605e-01],\n",
       "       [1.11737966e+00],\n",
       "       [2.80773877e-01],\n",
       "       [3.96202978e-01],\n",
       "       [1.60148914e+00],\n",
       "       [1.93652315e+00],\n",
       "       [6.26848356e-01],\n",
       "       [1.38464523e+00],\n",
       "       [1.75277830e+00],\n",
       "       [1.78921333e+00],\n",
       "       [1.70088423e-01],\n",
       "       [7.81095665e-02],\n",
       "       [3.39660839e-01],\n",
       "       [1.75628501e+00],\n",
       "       [1.96693668e-01],\n",
       "       [8.42215250e-01],\n",
       "       [1.91577906e+00],\n",
       "       [1.06633057e+00],\n",
       "       [1.38375423e+00],\n",
       "       [6.31031262e-01],\n",
       "       [1.37300186e+00],\n",
       "       [1.66925134e+00],\n",
       "       [3.65765547e-02],\n",
       "       [1.50028863e+00],\n",
       "       [1.97772218e+00],\n",
       "       [1.49633131e+00],\n",
       "       [5.60887984e-01],\n",
       "       [1.57855866e+00],\n",
       "       [2.06452013e-01],\n",
       "       [8.95787052e-01],\n",
       "       [1.81719101e+00],\n",
       "       [5.87228297e-01],\n",
       "       [5.75550677e-01],\n",
       "       [2.60057144e-01],\n",
       "       [3.87339157e-02],\n",
       "       [1.35767107e+00],\n",
       "       [4.23256232e-01],\n",
       "       [5.31093319e-01],\n",
       "       [9.83146319e-01],\n",
       "       [1.06725090e-01],\n",
       "       [1.14823521e+00],\n",
       "       [2.93457150e-01],\n",
       "       [1.17861107e+00],\n",
       "       [1.39951672e+00],\n",
       "       [2.04668858e-01],\n",
       "       [8.28111976e-01],\n",
       "       [1.38880032e+00],\n",
       "       [8.28358539e-01],\n",
       "       [9.99069179e-02],\n",
       "       [1.07179281e+00],\n",
       "       [1.32758929e+00],\n",
       "       [1.02977822e+00],\n",
       "       [1.88918951e+00],\n",
       "       [1.17311008e+00],\n",
       "       [1.80680383e+00],\n",
       "       [2.74949408e-01],\n",
       "       [2.78552695e-01],\n",
       "       [1.61478258e+00],\n",
       "       [7.95353674e-01],\n",
       "       [3.30708394e-01],\n",
       "       [1.85501716e+00],\n",
       "       [6.95531719e-01],\n",
       "       [1.50162421e+00],\n",
       "       [1.45199597e+00],\n",
       "       [1.76661218e+00],\n",
       "       [1.24734441e+00],\n",
       "       [1.50188487e+00],\n",
       "       [6.97796684e-01],\n",
       "       [5.39855784e-01],\n",
       "       [1.79177244e+00],\n",
       "       [8.56182380e-01],\n",
       "       [1.92968009e+00],\n",
       "       [1.32688300e+00],\n",
       "       [1.24339144e+00],\n",
       "       [2.29491946e-01],\n",
       "       [1.89897852e+00],\n",
       "       [8.99824267e-01],\n",
       "       [1.15677923e+00],\n",
       "       [8.16273606e-01],\n",
       "       [4.74053960e-01],\n",
       "       [1.80675904e+00],\n",
       "       [1.14735897e+00],\n",
       "       [5.74065406e-03],\n",
       "       [1.23428983e+00]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-29T07:22:57.866566Z",
     "start_time": "2025-06-29T07:22:57.853601Z"
    }
   },
   "cell_type": "code",
   "source": "X_b",
   "id": "2fbca0d694508498",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.00000000e+00, 8.34044009e-01],\n",
       "       [1.00000000e+00, 1.44064899e+00],\n",
       "       [1.00000000e+00, 2.28749635e-04],\n",
       "       [1.00000000e+00, 6.04665145e-01],\n",
       "       [1.00000000e+00, 2.93511782e-01],\n",
       "       [1.00000000e+00, 1.84677190e-01],\n",
       "       [1.00000000e+00, 3.72520423e-01],\n",
       "       [1.00000000e+00, 6.91121454e-01],\n",
       "       [1.00000000e+00, 7.93534948e-01],\n",
       "       [1.00000000e+00, 1.07763347e+00],\n",
       "       [1.00000000e+00, 8.38389029e-01],\n",
       "       [1.00000000e+00, 1.37043900e+00],\n",
       "       [1.00000000e+00, 4.08904499e-01],\n",
       "       [1.00000000e+00, 1.75623487e+00],\n",
       "       [1.00000000e+00, 5.47751864e-02],\n",
       "       [1.00000000e+00, 1.34093502e+00],\n",
       "       [1.00000000e+00, 8.34609605e-01],\n",
       "       [1.00000000e+00, 1.11737966e+00],\n",
       "       [1.00000000e+00, 2.80773877e-01],\n",
       "       [1.00000000e+00, 3.96202978e-01],\n",
       "       [1.00000000e+00, 1.60148914e+00],\n",
       "       [1.00000000e+00, 1.93652315e+00],\n",
       "       [1.00000000e+00, 6.26848356e-01],\n",
       "       [1.00000000e+00, 1.38464523e+00],\n",
       "       [1.00000000e+00, 1.75277830e+00],\n",
       "       [1.00000000e+00, 1.78921333e+00],\n",
       "       [1.00000000e+00, 1.70088423e-01],\n",
       "       [1.00000000e+00, 7.81095665e-02],\n",
       "       [1.00000000e+00, 3.39660839e-01],\n",
       "       [1.00000000e+00, 1.75628501e+00],\n",
       "       [1.00000000e+00, 1.96693668e-01],\n",
       "       [1.00000000e+00, 8.42215250e-01],\n",
       "       [1.00000000e+00, 1.91577906e+00],\n",
       "       [1.00000000e+00, 1.06633057e+00],\n",
       "       [1.00000000e+00, 1.38375423e+00],\n",
       "       [1.00000000e+00, 6.31031262e-01],\n",
       "       [1.00000000e+00, 1.37300186e+00],\n",
       "       [1.00000000e+00, 1.66925134e+00],\n",
       "       [1.00000000e+00, 3.65765547e-02],\n",
       "       [1.00000000e+00, 1.50028863e+00],\n",
       "       [1.00000000e+00, 1.97772218e+00],\n",
       "       [1.00000000e+00, 1.49633131e+00],\n",
       "       [1.00000000e+00, 5.60887984e-01],\n",
       "       [1.00000000e+00, 1.57855866e+00],\n",
       "       [1.00000000e+00, 2.06452013e-01],\n",
       "       [1.00000000e+00, 8.95787052e-01],\n",
       "       [1.00000000e+00, 1.81719101e+00],\n",
       "       [1.00000000e+00, 5.87228297e-01],\n",
       "       [1.00000000e+00, 5.75550677e-01],\n",
       "       [1.00000000e+00, 2.60057144e-01],\n",
       "       [1.00000000e+00, 3.87339157e-02],\n",
       "       [1.00000000e+00, 1.35767107e+00],\n",
       "       [1.00000000e+00, 4.23256232e-01],\n",
       "       [1.00000000e+00, 5.31093319e-01],\n",
       "       [1.00000000e+00, 9.83146319e-01],\n",
       "       [1.00000000e+00, 1.06725090e-01],\n",
       "       [1.00000000e+00, 1.14823521e+00],\n",
       "       [1.00000000e+00, 2.93457150e-01],\n",
       "       [1.00000000e+00, 1.17861107e+00],\n",
       "       [1.00000000e+00, 1.39951672e+00],\n",
       "       [1.00000000e+00, 2.04668858e-01],\n",
       "       [1.00000000e+00, 8.28111976e-01],\n",
       "       [1.00000000e+00, 1.38880032e+00],\n",
       "       [1.00000000e+00, 8.28358539e-01],\n",
       "       [1.00000000e+00, 9.99069179e-02],\n",
       "       [1.00000000e+00, 1.07179281e+00],\n",
       "       [1.00000000e+00, 1.32758929e+00],\n",
       "       [1.00000000e+00, 1.02977822e+00],\n",
       "       [1.00000000e+00, 1.88918951e+00],\n",
       "       [1.00000000e+00, 1.17311008e+00],\n",
       "       [1.00000000e+00, 1.80680383e+00],\n",
       "       [1.00000000e+00, 2.74949408e-01],\n",
       "       [1.00000000e+00, 2.78552695e-01],\n",
       "       [1.00000000e+00, 1.61478258e+00],\n",
       "       [1.00000000e+00, 7.95353674e-01],\n",
       "       [1.00000000e+00, 3.30708394e-01],\n",
       "       [1.00000000e+00, 1.85501716e+00],\n",
       "       [1.00000000e+00, 6.95531719e-01],\n",
       "       [1.00000000e+00, 1.50162421e+00],\n",
       "       [1.00000000e+00, 1.45199597e+00],\n",
       "       [1.00000000e+00, 1.76661218e+00],\n",
       "       [1.00000000e+00, 1.24734441e+00],\n",
       "       [1.00000000e+00, 1.50188487e+00],\n",
       "       [1.00000000e+00, 6.97796684e-01],\n",
       "       [1.00000000e+00, 5.39855784e-01],\n",
       "       [1.00000000e+00, 1.79177244e+00],\n",
       "       [1.00000000e+00, 8.56182380e-01],\n",
       "       [1.00000000e+00, 1.92968009e+00],\n",
       "       [1.00000000e+00, 1.32688300e+00],\n",
       "       [1.00000000e+00, 1.24339144e+00],\n",
       "       [1.00000000e+00, 2.29491946e-01],\n",
       "       [1.00000000e+00, 1.89897852e+00],\n",
       "       [1.00000000e+00, 8.99824267e-01],\n",
       "       [1.00000000e+00, 1.15677923e+00],\n",
       "       [1.00000000e+00, 8.16273606e-01],\n",
       "       [1.00000000e+00, 4.74053960e-01],\n",
       "       [1.00000000e+00, 1.80675904e+00],\n",
       "       [1.00000000e+00, 1.14735897e+00],\n",
       "       [1.00000000e+00, 5.74065406e-03],\n",
       "       [1.00000000e+00, 1.23428983e+00]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-29T07:22:57.898481Z",
     "start_time": "2025-06-29T07:22:57.876540Z"
    }
   },
   "cell_type": "code",
   "source": "y",
   "id": "fa991f27916ac7f8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6.81776698],\n",
       "       [ 6.29974574],\n",
       "       [ 3.69448224],\n",
       "       [ 6.64197008],\n",
       "       [ 5.11063008],\n",
       "       [ 5.31604275],\n",
       "       [ 4.89523313],\n",
       "       [ 5.87260629],\n",
       "       [ 6.56716624],\n",
       "       [ 7.64295205],\n",
       "       [ 6.71346681],\n",
       "       [ 8.23032565],\n",
       "       [ 4.55605121],\n",
       "       [ 9.6462684 ],\n",
       "       [ 4.28614683],\n",
       "       [ 9.15228897],\n",
       "       [ 7.70274669],\n",
       "       [ 7.53729539],\n",
       "       [ 4.46703668],\n",
       "       [ 4.54987853],\n",
       "       [ 9.22796177],\n",
       "       [ 9.88690952],\n",
       "       [ 5.53669139],\n",
       "       [ 8.19753255],\n",
       "       [ 8.63833407],\n",
       "       [10.06567202],\n",
       "       [ 4.0631367 ],\n",
       "       [ 5.4588364 ],\n",
       "       [ 5.42247416],\n",
       "       [ 9.86243354],\n",
       "       [ 3.49516916],\n",
       "       [ 6.69602818],\n",
       "       [10.48789363],\n",
       "       [ 6.24529111],\n",
       "       [ 7.88504418],\n",
       "       [ 5.92570833],\n",
       "       [ 6.74588825],\n",
       "       [ 9.32291342],\n",
       "       [ 4.95589031],\n",
       "       [ 7.64134995],\n",
       "       [10.28371251],\n",
       "       [ 7.17671052],\n",
       "       [ 5.64396844],\n",
       "       [ 7.11990362],\n",
       "       [ 5.74077375],\n",
       "       [ 7.09626169],\n",
       "       [ 9.42695606],\n",
       "       [ 4.98652327],\n",
       "       [ 7.00040796],\n",
       "       [ 6.74727318],\n",
       "       [ 2.25821988],\n",
       "       [ 9.30917723],\n",
       "       [ 6.89741945],\n",
       "       [ 5.93129165],\n",
       "       [ 5.75017092],\n",
       "       [ 5.18352059],\n",
       "       [ 7.26378533],\n",
       "       [ 4.27645082],\n",
       "       [ 6.30577509],\n",
       "       [ 8.74908766],\n",
       "       [ 5.40681344],\n",
       "       [ 5.8608052 ],\n",
       "       [ 8.68697728],\n",
       "       [ 5.34073423],\n",
       "       [ 5.10158179],\n",
       "       [ 7.26194573],\n",
       "       [ 7.7961981 ],\n",
       "       [ 6.9875888 ],\n",
       "       [10.53645469],\n",
       "       [ 8.26974188],\n",
       "       [ 9.94987682],\n",
       "       [ 4.96254943],\n",
       "       [ 4.91347921],\n",
       "       [ 9.46272799],\n",
       "       [ 6.61855558],\n",
       "       [ 5.67467659],\n",
       "       [ 9.25493471],\n",
       "       [ 3.65175739],\n",
       "       [ 9.54369722],\n",
       "       [10.54296756],\n",
       "       [ 9.74120099],\n",
       "       [ 7.64187801],\n",
       "       [ 8.36920986],\n",
       "       [ 5.97433586],\n",
       "       [ 5.63697676],\n",
       "       [ 8.25329858],\n",
       "       [ 6.05145268],\n",
       "       [ 8.79201346],\n",
       "       [ 8.22944815],\n",
       "       [ 7.43353317],\n",
       "       [ 5.18368716],\n",
       "       [ 9.52223239],\n",
       "       [ 7.68580799],\n",
       "       [ 7.68387159],\n",
       "       [ 8.63952055],\n",
       "       [ 3.52580096],\n",
       "       [ 8.77336044],\n",
       "       [ 8.34356381],\n",
       "       [ 6.54554767],\n",
       "       [ 7.4542347 ]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-29T07:22:57.959318Z",
     "start_time": "2025-06-29T07:22:57.945357Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建超参数\n",
    "learning_rate = 0.001\n",
    "n_iterations = 10000"
   ],
   "id": "7152ada4deff72d3",
   "outputs": [],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-29T07:22:58.052070Z",
     "start_time": "2025-06-29T07:22:57.961314Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#  （步骤一）初始化θ, W0...Wn，标准正太分布创建W\n",
    "theta = np.random.randn(2, 1)\n",
    "theta"
   ],
   "id": "aa1febd03f84656e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.04366899],\n",
       "       [-0.22631424]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "$$\n",
    "gradient_i= X^T \\cdot (X\\theta-y)\\\\\n",
    "$$\n"
   ],
   "id": "47b76081d6883ae2"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-29T07:22:58.159049Z",
     "start_time": "2025-06-29T07:22:58.057057Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# （步骤四）不会设置阈值，直接设置超参数，迭代次数，迭代次数到了，我们就认为收敛了\n",
    "for _ in range(n_iterations):\n",
    "    # 接着步骤二，计算梯度\n",
    "    gradients = X_b.T.dot(X_b.dot(theta) - y)\n",
    "    # 接着步骤三，应用公式调整 θ 值\n",
    "    theta = theta - learning_rate * gradients\n",
    "\n",
    "theta"
   ],
   "id": "28e366d5afc14776",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4.23695725],\n",
       "       [2.84246254]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-29T07:22:58.175007Z",
     "start_time": "2025-06-29T07:22:58.162041Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "c93859851cdc9b4a",
   "outputs": [],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-29T07:22:58.190965Z",
     "start_time": "2025-06-29T07:22:58.179993Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "572b0bfa399aef01",
   "outputs": [],
   "execution_count": 36
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
